package cn.iocoder.yudao.module.member.service.faceanalyserequest;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;

import cn.iocoder.yudao.module.member.dal.dataobject.faceanalyseresult.FaceanalyseResultDO;
import cn.iocoder.yudao.module.member.dal.mysql.faceanalyseresult.FaceanalyseResultMapper;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.faceanalyserequest.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.faceanalyserequest.FaceanalyseRequestDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.faceanalyserequest.FaceanalyseRequestMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 会员皮肤状态分析请求 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FaceanalyseRequestServiceImpl implements FaceanalyseRequestService {

    @Resource
    private FaceanalyseRequestMapper faceanalyseRequestMapper;
    @Resource
    private FaceanalyseResultMapper faceanalyseResultMapper;

    @Override
    public FaceanalyseResultDO createFaceanalyseRequest(FaceanalyseRequestSaveReqVO createReqVO) throws JsonProcessingException {
        // 插入
        FaceanalyseRequestDO faceanalyseRequest = BeanUtils.toBean(createReqVO, FaceanalyseRequestDO.class);
        faceanalyseRequestMapper.insert(faceanalyseRequest);
        //插入过后将数据访问到皮肤分析

        //将对象名称驼峰转下划线
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 忽略所有为null的属性
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String jsonParam= mapper.writeValueAsString(createReqVO);

        //将json转为map
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.putAll(mapper.readValue(jsonParam, HashMap.class));
        System.out.println("请求参数==========="+paramMap);

        String result= HttpUtil.post("https://api-cn.faceplusplus.com/facepp/v1/skinanalyze_pro", paramMap);
        System.out.println("返回结果==========="+result);
        FaceanalyseResultDO vo= JSON.parseObject( result,FaceanalyseResultDO.class);
        vo.setRequestOwnId(faceanalyseRequest.getId());
        vo.setUserId(createReqVO.getUserId());
        faceanalyseResultMapper.insert(vo);
        // 返回
        return vo;
    }

    @Override
    public void updateFaceanalyseRequest(FaceanalyseRequestSaveReqVO updateReqVO) {
        // 校验存在
        validateFaceanalyseRequestExists(updateReqVO.getId());
        // 更新
        FaceanalyseRequestDO updateObj = BeanUtils.toBean(updateReqVO, FaceanalyseRequestDO.class);
        faceanalyseRequestMapper.updateById(updateObj);
    }

    @Override
    public void deleteFaceanalyseRequest(Long id) {
        // 校验存在
        validateFaceanalyseRequestExists(id);
        // 删除
        faceanalyseRequestMapper.deleteById(id);
    }

    @Override
        public void deleteFaceanalyseRequestListByIds(List<Long> ids) {
        // 删除
        faceanalyseRequestMapper.deleteByIds(ids);
        }


    private void validateFaceanalyseRequestExists(Long id) {
        if (faceanalyseRequestMapper.selectById(id) == null) {
            throw exception(FACEANALYSE_REQUEST_NOT_EXISTS);
        }
    }

    @Override
    public FaceanalyseRequestDO getFaceanalyseRequest(Long id) {
        return faceanalyseRequestMapper.selectById(id);
    }

    @Override
    public PageResult<FaceanalyseRequestDO> getFaceanalyseRequestPage(FaceanalyseRequestPageReqVO pageReqVO) {
        return faceanalyseRequestMapper.selectPage(pageReqVO);
    }

}